/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package digitaldreamers.libraries.games.clients.gameclient.gameui.implementation;

import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IAudio;
import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IInput;
import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IScreen;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author DaoLe
 */
public abstract class GameScreen extends JPanel implements IScreen {
    
    protected WindowsGame m_Game = null;
    protected RenderPanel m_RenderPanel = null;
    protected IAudio m_Audio = null;
    protected Image m_BackgroundImage = null;
    protected Color m_BackgroundColor = null;
    protected BufferedImage m_BufferedImage = null;
    protected Graphics2D m_Graphics2D = null;
    
    public GameScreen(RenderPanel renderPanel) {
        this(renderPanel.getWidth(), renderPanel.getHeight(), renderPanel);
    }
    
    public GameScreen(int iScreenWidth, int iScreenHeight, RenderPanel renderPanel) {
        m_Game = renderPanel.getGame();
        m_RenderPanel = renderPanel;
        m_Audio = m_Game.getAudio();
        m_BufferedImage = new BufferedImage(iScreenWidth, iScreenHeight, BufferedImage.TYPE_4BYTE_ABGR);
        m_Graphics2D = m_BufferedImage.createGraphics();
        setSize(iScreenWidth, iScreenHeight);
        setVisible(true);
    }
    
    @Override
    public Image getBackgroundImage() {
        return m_BackgroundImage;
    }
    
    @Override
    public synchronized void setBackgroundImage(Image backgroundImage) {
        m_BackgroundImage = backgroundImage;
    }
    
    @Override
    public Color getBackgroundColor() {
        return m_BackgroundColor;
    }
    
    @Override
    public synchronized void setBackgroundColor(Color backgroundColor) {
        m_BackgroundColor = backgroundColor;
    }
    
    public synchronized BufferedImage getBufferedImage() {
        return m_BufferedImage;
    }
    
    public WindowsGame getGame() {
        return m_Game;
    }
    
    public RenderPanel getRenderPanel() {
        return m_RenderPanel;
    }
    
    public Graphics2D getGraphics2D() {
        return m_Graphics2D;
    }
    
    protected boolean inBounds(IInput.TouchEvent event, int iX, int iY, int iWidth, int iHeight) {
        return event.getX() > iX && event.getX() < iX + iWidth - 1 && event.getY() > iY && event.getY() < iY + iHeight - 1;
    }
    
    @Override
    protected void paintComponent(Graphics grphcs) {
        super.paintComponent(grphcs);
        Color backgroundColor = Color.BLACK;
        if(m_BackgroundColor != null)  {
            backgroundColor = m_BackgroundColor;
        }
        grphcs.setColor(backgroundColor);
        grphcs.fillRect(0, 0, m_BufferedImage.getWidth(), m_BufferedImage.getHeight());
        if(m_BackgroundImage != null) {
            grphcs.drawImage(m_BackgroundImage, 0, 0, m_BufferedImage.getWidth(), m_BufferedImage.getHeight(), 0, 0, m_BackgroundImage.getWidth(null), m_BackgroundImage.getHeight(null), null);
        }
        paintScreen(grphcs);
    }
    
    @Override
    public synchronized void present(long lDeltaTime) {
        paint(m_Graphics2D);
        //paintScreen(lDeltaTime, m_Graphics2D);
    }
    
    public abstract void paintScreen(Graphics grphcs);
    //public abstract void paintScreen(long lDeltaTime, Graphics grphcs);
}
